#pragma once
#include <string>
#include<iostream>
#include<jsoncpp/json/json.h>
//#define Myself 1 //用来条件编译,可以写在makefile
const std::string space_separator = " "; // 空格分隔符
const std::string protocol_sept = "\n";

//协议结构体里面质只包含变量，数据，有效载荷？ 并没有头尾
std::string Encode(const std::string &content) // 封装
{
    std::string s = std::to_string(content.size());
    s += protocol_sept;
    s += content;
    s += protocol_sept;
    return s;
    
}

//"9"\n"100 + 200"\n"
bool Decode(std::string &package, std::string *content) // 解包
{
    size_t pos = package.find(protocol_sept);
    if (pos == std::string::npos)
        return false;
    std::string len_str = package.substr(0, pos);
    size_t len = stoi(len_str);
    size_t total_len = len_str.size() + len + 2;
    if (package.size() < total_len)
        return false;
    *content = package.substr(pos + 1, len);//这里没有头尾只要中间的数据（用来反序列化）
    package.erase(0, total_len); // 获取到一个完整的报文后就删除package里的一个报文
    return true;
}
class Request
{
public:
    Request()
    {
    }
    Request(const int &data1, const int &data2, const char &opera)
        : x_(data1), y_(data2), op_(opera)
    {
    }

    bool Serialize(std::string *out) // 序列化： struct -> string,"x op y"
    {
    #ifdef Myself
        std::string s = std::to_string(x_);
        s += space_separator;
        s += op_;
        s += space_separator;
        s += std::to_string(y_);
        *out = s;
        return true;
    #else     //json 序列化
    
        Json::Value root;
        root["x"]=x_;
        root["y"]=y_;
        root["op"]=op_;
        Json::StyledWriter w;
        *out=w.write(root);
        return true;
    #endif
    }
    bool Deserialize(const std::string &in) // 反序列化： "x op y" -> struct
    {
    #ifdef Myself
        // 把输入的字符串分割然后再转结构体数据
        size_t left = in.find(space_separator);
        if (left == std::string::npos)
            return false;
        std::string part_x = in.substr(0, left);
        size_t right = in.rfind(space_separator);
        if (left == std::string::npos)
            return false;
        std::string part_y = in.substr(right + 1);
        if (left + 2 != right)
            return false;
        op_ = in[left + 1];
        x_ = stoi(part_x);
        y_ = stoi(part_y);
        return true;
    #else
        //json 反序列化
        Json::Value root;
        Json::Reader r;
        r.parse(in,root);
        x_=root["x"].asInt();
        y_=root["y"].asInt();
        op_=root["op"].asInt();
        return true;

    #endif
    }
    void DebugPrint()
    {
        std::cout<<"请求构建完成-> " << "x op y is:" << x_ << " " << op_ << " " << y_ << std::endl;
    }

public:
    int x_;
    int y_;
    char op_;
};

class Response
{
public:
    Response()
    {
    }
    Response(int result, int code)
        : result_(result), code_(code)
    {
    }
    bool Serialize(std::string *out) //"result_ code_"
    {
    #ifdef Myself
        std::string s = std::to_string(result_);
        s += space_separator;
        s += std::to_string(code_);
        *out = s;
        return true;
    #else
    Json::Value root;
    root["result"]=result_;
    root["code"]=code_;
    Json::FastWriter w;
    *out=w.write(root);
    return true;
    #endif
    }
    bool Deserialize(const std::string &in)
    {
    #ifdef Myself
        size_t left = in.find(space_separator);
        if (left == std::string::npos)
            return false;
        std::string result = in.substr(0, left);
        std::string code = in.substr(left + 1);

        result_ = stoi(result);
        code_ = stoi(code);
        return true;
    #else
        Json::Value v;
        Json::Reader r;
        r.parse(in,v);//直接就反序列化到v结构体内了？？
        result_=v["result"].asInt();
        code_=v["code"].asInt();
        return true;

    #endif
    }
    void DebugPrint()
    {
        std::cout <<"响应构建完成->"<< "result is:" << result_ << " , code is: " << code_ << std::endl;
    }

public:
    int result_;
    int code_; // 运算错误码
};